package com.clement.theAndroidSalesman;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.clement.theAndroidSalesman.ui.Carte;

public class TheAndroidSalesman extends Activity {
	private static final int DIALOG_MENU = 1;
	private static final int DIALOG_CARTE = 2;
	private static final int DIALOG_RESULTAT = 3;

	private Carte carte;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pvc_layout);

		carte = (Carte) findViewById(R.id.carte);
		carte.setJeuPVC(this);

		Button boutonMenu = (Button) findViewById(R.id.boutonMenu);
		boutonMenu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(DIALOG_MENU);
			}
		});

		Button boutonEffacer = (Button) findViewById(R.id.boutonEffacer);
		boutonEffacer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				carte.effacer();
			}
		});
	}

	public void writeDistances(int distance, int meilleurDistance) {
		// Distance parcourue
		TextView labelDistance = (TextView) findViewById(R.id.labelDistance);
		TextView labelMeilleurDistance = (TextView) findViewById(R.id.labelMeilleur);
		if (labelDistance != null) {
			labelDistance.setText(getResources().getString(R.string.distance) + String
					.valueOf(distance));
		}
		if (labelMeilleurDistance != null)
			if (meilleurDistance != Integer.MAX_VALUE) {
				labelMeilleurDistance.setText(getResources().getString(R.string.meilleure) + String
						.valueOf(meilleurDistance));
			} else {
				labelMeilleurDistance.setText(getResources().getString(R.string.meilleure));
			}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		final Dialog dialog;
		switch (id) {
		case DIALOG_MENU:
			dialog = new Dialog(this);
			dialog.setContentView(R.layout.menu_dialog);
			dialog.setTitle(R.string.app_name);
			Button boutonSolution = (Button) dialog.findViewById(R.id.boutonSolution);
			Button boutonNouvelleCarte = (Button) dialog.findViewById(R.id.boutonNouvelleCarte);
			Button boutonQuitter = (Button) dialog.findViewById(R.id.boutonQuitter);
			boutonSolution.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// showDialog(DIALOG_RESULTAT);
					dialog.dismiss();
				}
			});
			boutonNouvelleCarte.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					showDialog(DIALOG_CARTE);
					dialog.dismiss();
				}
			});
			boutonQuitter.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					TheAndroidSalesman.this.finish();
				}
			});
			break;
		case DIALOG_CARTE:
			dialog = new Dialog(this);
			dialog.setContentView(R.layout.carte_dialog);
			dialog.setTitle("Combien de villes sur la carte ?");
			final SeekBar bar = (SeekBar) dialog.findViewById(R.id.nombreVilleBar);
			final TextView text = (TextView) dialog.findViewById(R.id.nombreVilleText);
			Button bouton = (Button) dialog.findViewById(R.id.nombreVilleBouton);
			bar.setMax(30);
			bar.setProgress(carte.getNbVille());
			text.setText(String.valueOf(carte.getNbVille()));
			bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
				}

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
					if (progress < 3) {
						bar.setProgress(3);
					}
					text.setText(String.valueOf(bar.getProgress()));
				}
			});
			bouton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (bar.getProgress() >= 3) {
						carte.initCarte(bar.getProgress());
						carte.invalidate();
						dialog.dismiss();
					}
				}
			});
			break;
		// case DIALOG_RESULTAT:
		// IChromosome optimal = null;
		// try {
		// Configuration.reset();
		// Resolution r = new Resolution(carte.getVilles());
		// optimal = r.findOptimalPath(null);
		// } catch (Exception ex) {
		// ex.printStackTrace();
		// }
		// AlertDialog.Builder builder = new AlertDialog.Builder(this);
		// builder.setMessage(
		// optimal.toString() + " Score "
		// + (Integer.MAX_VALUE / 2 - optimal.getFitnessValue()))
		// .setNeutralButton("OK", new DialogInterface.OnClickListener() {
		// public void onClick(DialogInterface dialog, int id) {
		// dialog.dismiss();
		// }
		// });
		// dialog = builder.create();
		// break;
		default:
			dialog = null;
			break;
		}
		return dialog;
	}
}
